import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;

public class Admin {
	static ObjectOutputStream out;
	static ObjectInputStream in;
	static ArrayList<Book> Books;
	private static Socket s;
	private String password;
	private static Boolean status;
	
	public static Boolean isConnected(){return status;}
	
	public Admin(String password){this.password=password;}

	private Object receiveObject ( ) throws ClassNotFoundException
	{
		Object received = null;

		try{
			received = in.readObject();
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		}

		return received;
	}

	private void sendObject ( Object obj)
	{
		try{
			out.writeObject(obj);
			out.flush();
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		}
	}
	
	public void send(Object o) throws ClassNotFoundException{
		connect();
		sendObject(o);
		disconnect();
	}
	
	public Object receive(Object o) throws ClassNotFoundException{
		connect();
		Object ob = receiveObject();
		disconnect();
		return ob;
	}
	
	private void connect() throws ClassNotFoundException{
		System.out.println("Initializing Client.");
	       
        try {
            System.out.println("Connecting on Library Server.");    
        	s = new Socket("localhost", 20009);
        	System.out.println("You are connected on Library.");
        	
    		out = new ObjectOutputStream(s.getOutputStream());
    		in = new ObjectInputStream(s.getInputStream());
    		
    		sendObject(new Integer(0));
    		
    		sendObject(password);
    		
    		String result = receiveObject().toString();
    		
    		status = (Boolean) receiveObject();
    		
    		System.out.println(result);
              
        } catch (IOException e) {
            System.out.println("Troubles on Server => " + e.getMessage());
		}
	}
	
	private void disconnect(){
        try {
        	//System.out.println("OOOOOOOKAAA");
			s.close();
		} catch (IOException e) {
			System.out.println("Troubles on Server => " + e.getMessage());
		}
	}
	
	public void searchBooks(){
		try{
			connect();
			sendObject(1);
			ArrayList<Book> books = (ArrayList<Book>)receiveObject();
			for(Book b : books){
				b.showBook();
			}
			disconnect();
		} catch (Exception e){}
	}

	public void subscribeBook(int id, String title, int year){
		try{
			Book b = new Book(id, title, year);
			connect();
			sendObject(2);
			//System.out.println("AKIIIIIIIIIIII");
			sendObject(b);
			//System.out.println("AKIIIIIIIIIIII");
			disconnect();
		} catch (Exception e){}
	}
	
	public void unsubscribeBook(int id){
		try{
			connect();
			sendObject(3);
			sendObject(new Book(id));
			String response = receiveObject().toString();
			disconnect();
			System.out.println(response);
		} catch (Exception e) {}
	}
	
	public ArrayList<Book> getBooks(){
		ArrayList<Book> books = null;
		try{
			connect();
			sendObject(1);
			books = (ArrayList<Book>)receiveObject();
			disconnect();
		} catch (Exception e){}
		return books;
	}
	
}